package com.example.attempt.controller;

import com.example.attempt.entity.Course;
import com.example.attempt.entity.SelectCourse;
import com.example.attempt.entity.Student;
import com.example.attempt.service.impl.CourseServiceImpl;
import com.example.attempt.service.impl.SelectCourseServiceImpl;
import com.example.attempt.service.impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SelectCourseController {

    @Autowired
    private SelectCourseServiceImpl selectCourseImpl;
    @Autowired
    private CourseServiceImpl courseImpl;
    @Autowired
    private StudentServiceImpl studentImpl;
    @PostMapping(path = "/getStudents")
    public List<Student> getStudents(String cid){
        System.out.println(selectCourseImpl.getStudents(cid).toString());

        return selectCourseImpl.getStudents(cid);
    }
    @RequestMapping(path = "/getAllSelect")
    public List<SelectCourse> getAllSelect(){
        return selectCourseImpl.queryAll();
    }

    @PostMapping(path = "/getSelectByCid")
    public List<SelectCourse> queryByCid(@RequestParam(value = "cid") String cid){
        return selectCourseImpl.queryByCid(cid);
    }
    @PostMapping(path = "/getSelectBySid")
    public List<Course> queryBySid(@RequestParam(value = "sid") String sid){
        List<SelectCourse> courses=selectCourseImpl.queryBySid(sid);
        List<Course> courses1=new ArrayList<>();
        System.out.println(courses.toString());
        for (SelectCourse c : courses) {
            System.out.println(c.toString());
            Course cc = courseImpl.queryById(c.getCid());
            System.out.println(cc.toString());
            courses1.add(cc);
        }
        return courses1;
    }

    public int addStudentToCourse(SelectCourse selectCourse){
        if(courseImpl.queryById(selectCourse.getCid())==null){
            return 0; //不存在课堂号
        }
        if (studentImpl.queryById(selectCourse.getSid())==null){
            return -1;//不存在这个学生
        }
        if(selectCourseImpl.queryOne(selectCourse)!=null){
            return -2;//存在该条选课记录
        }
        else {
            selectCourseImpl.addStudentToCourse(selectCourse);
            return 1;
        }
    }

    @PostMapping(path = "/addStudentToCourseById")
    public int addStudentToCourseById(@RequestBody SelectCourse selectCourse){
        if (studentImpl.queryById(selectCourse.getSid())==null){
            return -1;//学生不存在
        }
        if (courseImpl.queryById(selectCourse.getCid())==null){
            return 0;//课程不存在
        }
        System.out.println("进来添加选课了捏");
        return addStudentToCourse(selectCourse);
    }

    @PostMapping(path = "/addStudentToCourseByName")
    public int addStudentToCourseByName(@RequestParam(value = "sname") String sname, @RequestParam(value = "cid") String cid){
        System.out.println("进来添加选课了捏");
        List<Student> students = studentImpl.queryByName(sname);
        if (students.isEmpty()){
            return -1;//学生不存在
        }
        if (courseImpl.queryById(cid)==null){
            return 0;//课程不存在
        }

        SelectCourse selectCourse = new SelectCourse();
        for (Student student : students) {
            String sid = student.getSid();
            selectCourse.setSid(sid);
            selectCourse.setCid(cid);
            addStudentToCourse(selectCourse);
        }
        return 1;
    }

    @PostMapping(path = "/deleteStudentFromCourseById")
    public int deleteStudentFromCourseById(@RequestBody SelectCourse selectCourse){
        System.out.println(selectCourse.toString());
        if(selectCourseImpl.queryOne(selectCourse)==null){
            return 0;//不存在该条选课记录
        }
        else {
            selectCourseImpl.deleteStudentFromCourse(selectCourse);
            return 1;
        }
    }

}
